$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'capital/merchantapplycash/smallBCheckList',
        datatype: "json",
        colModel: [
            {label: '申请id', name: 'id', index: 'id', width: 60, key: true},
            {label: '联动单号', name: 'payNo', index: 'pay_no', width: 80},
            {label: '商户', name: 'merchantName', index: 'merchant_name', width: 130},
            {label: '提现类型', name: 'applyTypeText', index: 'apply_type', width: 80},
            {label: '申请状态', align: 'center', name: 'applyStatusText', index: 'apply_status', width: 80},
            {label: '提现金额', align: 'center', name: 'applyMoney', index: 'apply_money', width: 80},
            {label: '提现汇费', align: 'center', name: 'applyFee', index: 'apply_fee', width: 80},
            {label: '预计到账日期', align: 'center', name: 'expectDate', index: 'expect_date', width: 130},
            {label: '实际到账时间', align:'center', name: 'actualTime', index: 'actual_time', width: 130},
            {label: '申请人', name: 'applyUser', index: 'apply_user', width: 80,},
            {label: '申请时间',align:'center', name: 'applyTime', index: 'apply_time', width: 130},
            {label: '审核人', name: 'auditor', index: 'auditor', width: 80},
            {label: '审核时间',align:'center', name: 'auditTime', index: 'audit_time', width: 130},
            {label: '备注', name: 'remark', index: 'remark', width: 80},
            {
                label: '操作',align:'center', name: 'applyStatus', index: 'apply_status', width: 80,
                formatter: function (value, options, row) {
                    if (value == "0") { //待审核
                        return '<a href="javascript:void(0);" onclick="return vm.goDetail(' + row.id + ')" >审核</a>';
                    } else { // 查看
                        return '<a href="javascript:void(0);" onclick="return vm.goDetail(' + row.id + ')">查看</a>';
                    }
                }
            }
        ],
        viewrecords: true,
        height: 600,
        rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData",
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order",
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            console.log($("#jqGrid").jqGrid('getGridParam', 'userData'));
        }
    });
    $("#jqOrderItemsGrid").jqGrid({
        datatype: "local",
        colModel: [
            {label: '分配单号', name: 'orderAccountId', index: 'order_account_id', width: 80, key: true},
            {label: '订单号', name: 'orderId', index: 'order_id', width: 120},
            {label: '订单状态', name: 'orderStatus', index: 'order_status', width: 80},
            {label: '下单时间', name: 'orderCreateTime', index: 'order_create_time', width: 110},
            {label: '商品总额', name: 'totalAmount', index: 'total_amount', width: 80},
            {label: '配送费用', name: 'costFreight', index: 'cost_freight', width: 80},
            {label: '调价', name: 'discount', index: 'discount', width: 80},
            {label: '商品促销优惠', name: 'pmtGoods', index: 'pmt_goods', width: 80},
            {label: '优惠券促销', name: 'pmtOrder', index: 'pmt_order', width: 80},
            {label: '随机立减', name: 'pmtRandom', index: 'pmt_random', width: 80},
            {label: '订单应付金额', name: 'payableAsmount', index: 'payable_amount', width: 100},
            {label: '订单支付金额', name: 'payed', index: 'payed', width: 100},
            {label: '支付类型', name: 'payment', index: 'payment', width: 80},
            {label: '支付时间', name: 'paymentTime', index: 'payment_time', width: 80},
            {label: '订单完成时间', name: 'finishTime', index: 'finish_time', width: 100},
            {label: '订单提现汇费', name: 'fee', index: 'fee', width: 100},
            {label: '原订单号', name: 'originalOrderId', index: 'original_order_id', width: 100},
        ],
        viewrecords: true,
        rownumbers: true,
        rownumWidth: 50,
        autowidth: true,
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqOrderItemsGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    $("#jqOldOrderItemsGrid").jqGrid({
        datatype: "local",
        colModel: [
            {label: '订单号', name: 'orderId', index: 'order_id', width: 80, key: true},
            {label: '订单状态', name: 'orderStatus', index: 'order_status', width: 80},
            {label: '下单时间', name: 'orderCreateTime', index: 'order_create_time', width: 110},
            {label: '商品总额', name: 'totalAmount', index: 'total_amount', width: 80},
            {label: '配送费用', name: 'costFreight', index: 'cost_freight', width: 80},
            {label: '调价', name: 'discount', index: 'discount', width: 80},
            {label: '商品促销优惠', name: 'pmtGoods', index: 'pmt_goods', width: 80},
            {label: '优惠券促销', name: 'pmtOrder', index: 'pmt_order', width: 80},
            {label: '随机立减', name: 'pmtRandom', index: 'pmt_random', width: 80},
            {label: '订单应付金额', name: 'payableAsmount', index: 'payable_amount', width: 100},
            {label: '订单支付金额', name: 'payed', index: 'payed', width: 100},
            {label: '支付类型', name: 'payment', index: 'payment', width: 80},
            {label: '支付时间', name: 'paymentTime', index: 'payment_time', width: 80},
            {label: '订单完成时间', name: 'finishTime', index: 'finish_time', width: 100},
        ],
        viewrecords: true,
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqOrderItemsGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    $("#jqApplyCashLogGrid").jqGrid({
        datatype: "local",
        colModel: [
            {label: '单据编号', name: 'applyCashId', index: 'apply_cash_id', width: 100},
            {label: '日志状态', name: 'applyLogStatus', index: 'apply_log_status', width: 80,
                formatter: function (value, options, row) {
                    if (value == "0") { //待审核
                        return '支付中';
                    } else if(value == "1"){ // 查看
                        return '支付失败';
                    }else if (value == "2"){
                        return '支付成功';
                    }
                }
            },
            {label: '支付ID', name: 'applyPayId', index: 'apply_pay_id', width: 150},
            {label: '联动提现单号', name: 'payNo', index: 'pay_no', width: 150},
            {label: '提现信息', name: 'message', index: 'message', },
            {label: '银行卡号', name: 'bankNo', index: 'bank_no', width: 110},
            {label: '银行卡持有人', name: 'bankOwner', index: 'bank_owner', width: 100},
            {label: '银行', name: 'bank', index: 'bank', width: 150},
            {label: '预留手机', name: 'mobile', index: 'mobile', width: 80},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 110},
            {label: '备注', name: 'remark', index: 'remark',width: 110}
        ],
        rownumbers: true,
        rownumWidth: 50,
        autowidth: true,
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqApplyCashLogGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {id: null, payNo: null, sellerId: null},
        loading: false,
        sellerList:[],//商户列表
        showList: true,
        title: null,
        merchantApplyCash: {},
        applyStatusList: [
            {value: 0, label: '待审核'}, {value: 1, label: '审核通过'},
            {value: 2, label: '已驳回'}, {value: 3,label: '打款成功'},
            {value: 4, label: '打款失败'}
        ],
        searchResultVisible: false,//查询结果弹窗
        searchResult:{},//查询结果
    },
    methods: {
        query: function () {
            vm.reload();
        },
        goDetail: function (id) {
            vm.showList = false;
            vm.getApplyCashDetail(id);
        },
        //远程模糊查找工厂
        remoteMethod: function (query) {
            if (query !== '') {
                vm.loading = true;
                //远程请求
                $.get(baseURL + "capital/merchantapplycash/getSellerInfo?sellerType=0&keyWord=" + query, function (r) {
                    vm.loading = false;
                    if(r.code == 0){
                        vm.sellerList = r.sellerList;
                    }else {
                        alert(r.msg);
                    }
                });
            }else {
                vm.sellerList=[];
            }
        },
        //审核
        check: function () {
            if (!vm.merchantApplyCash.id) {
                alert("该单据id异常,请刷新重试!");
                return;
            }
            var self = this;
            this.$confirm('确定要审核该条记录吗, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                const loading = self.$loading({
                    lock: true,
                    text: '打款中,请勿刷新页面',
                    spinner: 'el-icon-loading'
                });
                $.ajax({
                    type: "POST",
                    url: baseURL + "capital/merchantapplycash/checkSmallB",
                    contentType: "application/json",
                    data: JSON.stringify(vm.merchantApplyCash.id),
                    success: function (r) {
                        loading.close();
                        if (r.code == 0) {
                            alert(r.msg, function (index) {
                                vm.goDetail(vm.merchantApplyCash.id);
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }).catch(() => {
                //loading.close();
            });


        },
        //驳回
        reject: function (event) {

        },
        //返回
        goBack: function () {
            vm.reload();
        },

        // 调用联动api 查看订单状态
        search: function () {
            var searchParam ={};
            if(!this.merchantApplyCash.applyPayId){
                alert("当前订单未生成联动支付单号,暂不支持查询!");
                return;
            }
            searchParam.orderId = this.merchantApplyCash.applyPayId;
            searchParam.merDate =  this.merchantApplyCash.applyTime;
            $.ajax({
                type: "POST",
                url: baseURL + "capital/merchantapplycash/searchUmfOrderStatus",
                contentType: "application/json",
                data: JSON.stringify(searchParam),
                success: function (r) {
                    if (r.code == 0) {
                        vm.searchResultVisible = true;
                        vm.searchResult = r.data;
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        // 跳转详情页
        getApplyCashDetail: function (id) {
            $.get(baseURL + "capital/merchantapplycash/info/" + id, function (r) {
                vm.merchantApplyCash = r.merchantApplyCash;

                //加载单据明细
                jQuery("#jqOrderItemsGrid").jqGrid("clearGridData");
                for (var i = 0; i < r.merchantApplyCashItems.length; i++) {
                    $("#jqOrderItemsGrid").jqGrid('addRowData', i + 1, r.merchantApplyCashItems[i]);
                }

                //加载原始单据明细
                jQuery("#jqOldOrderItemsGrid").jqGrid("clearGridData");
                for (var i = 0; i < r.oldMerchantApplyCashItems.length; i++) {
                    $("#jqOldOrderItemsGrid").jqGrid('addRowData', i + 1, r.oldMerchantApplyCashItems[i]);
                }


                //加载操作日志
                jQuery("#jqApplyCashLogGrid").jqGrid("clearGridData");
                if (r.merchantApplyLog) {
                    for (var i = 0; i < r.merchantApplyLog.length; i++) {
                        $("#jqApplyCashLogGrid").jqGrid('addRowData', i + 1, r.merchantApplyLog[i]);
                    }
                }
            });
        },
        //重载
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
        },

        //重置审核状态
        resetApplyStatus:function () {
            var self = this;
            this.$confirm('确认将订单状态置为待审核? 这将导致该订单可以再次被审核,打款!', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 业务
                const loading = self.$loading({
                    lock: true,
                    text: '重置审核状态中,请勿刷新页面',
                    spinner: 'el-icon-loading'
                });
                var searchParam ={};

                searchParam.id = vm.merchantApplyCash.id
                searchParam.orderId = vm.merchantApplyCash.applyPayId;
                searchParam.merDate =  vm.merchantApplyCash.applyTime;
                $.ajax({
                    type: "POST",
                    url: baseURL + "capital/merchantapplycash/resetApplyStatus",
                    contentType: "application/json",
                    data: JSON.stringify(searchParam),
                    success: function (r) {
                        loading.close();
                        if (r.code == 0) {
                            alert(r.msg, function (index) {
                                vm.goDetail(vm.merchantApplyCash.id);
                                vm.searchResultVisible = false;
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消确认打款'
                });
            });
        },
        //确认打款
        confirmCash: function () {
            var self = this;
            this.$confirm('确认客户已收到打款?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 业务
                const loading = self.$loading({
                    lock: true,
                    text: '确认打款中,请勿刷新页面',
                    spinner: 'el-icon-loading'
                });
                var searchParam ={};
                searchParam.id = vm.merchantApplyCash.id;
                searchParam.orderId = vm.merchantApplyCash.applyPayId;
                searchParam.merDate =  vm.merchantApplyCash.applyTime;
                $.ajax({
                    type: "POST",
                    url: baseURL + "capital/merchantapplycash/confirmCash",
                    contentType: "application/json",
                    data: JSON.stringify(searchParam),
                    success: function (r) {
                        loading.close();
                        if (r.code == 0) {
                            alert(r.msg, function (index) {
                                vm.goDetail(vm.merchantApplyCash.id);
                                vm.searchResultVisible = false;
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消确认打款'
                });
            });

        }
    },


});